#include "DisplayList.h"
#include "SceneObject.h"
#include "TimelineObject.h"
#include "LightAbstractRenderer.h"


namespace LightPlayer {


	void DisplayList::draw( LightAbstractRenderer* pRenderer )
	{
		SceneObject* Mask = NULL;

		display_list_type::iterator it = list.begin();
		for (; it != list.end(); ++it ){
			SceneObject* obj = it->second;
			
			// pop mask
			if( Mask && obj->GetDepth() > Mask->GetClipDepth() ){

				pRenderer->PopMask_Start();
				Mask->draw(pRenderer);
				pRenderer->PopMask_End();

				Mask = NULL;
			}
			// push mask
			if( obj->isMask() ){
				assert( Mask == NULL );
				Mask = obj;

				pRenderer->PushMask_Start();
				Mask->draw(pRenderer);
				pRenderer->PushMask_End();
			}
			// draw normal object
			if( ! obj->isMask() ){
				obj->draw(pRenderer);
			}
		}

		// pop mask
		if( Mask ){

			pRenderer->PopMask_Start();
			Mask->draw(pRenderer);
			pRenderer->PopMask_End();

			Mask = NULL;
		}
	}

};